package com.zzusedating.androidapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zzusedating.androidapp.R;
import com.zzusedating.androidapp.UserInfoActivity;
import com.zzusedating.androidapp.model.TweetItem;
import com.zzusedating.androidapp.tools.BasicCallback;
import com.zzusedating.androidapp.tools.BasicHandlerCallback;
import com.zzusedating.androidapp.tools.ConstantsTools;
import com.zzusedating.androidapp.tools.MyApplication;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class DiscoverAdapter extends RecyclerView.Adapter<DiscoverAdapter.ViewHolder> {
    private static final String TAG = "DiscoverAdapter";
    private ArrayList<TweetItem> items;
    private Context context;

    public DiscoverAdapter(Context context, ArrayList<TweetItem> items) {
        this.context = context;
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.recycler_item_discover, viewGroup, false));
        holder.setData(items.get(i));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.setData(items.get(i));
        final String id = items.get(i).getUserId();
//        viewHolder.followView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(final View v) {
//                MyApplication.getClient().newCall(
//                        new Request.Builder()
//                                .get()
//                                .url(String.format(ConstantsTools.FOLLOW_UN_USER, id))
//                                .build()
//                ).enqueue(
//                        new BasicCallback(
//                                new Handler(
//                                        new BasicHandlerCallback() {
//                                            @Override
//                                            public boolean handleMessage(Message msg) {
//                                                super.handleMessage(msg);
//                                                switch (msg.what) {
//                                                    case -1:
//                                                        MyApplication.Toast("未登录");
//                                                        break;
//                                                    case 5:
//                                                        MyApplication.Toast("用户不存在");
//                                                        break;
//                                                    case 1:
//                                                        ((TextView) v).setText("已关注");
//                                                        v.setClickable(true);
//                                                        break;
//                                                }
//                                                return true;
//                                            }
//                                        }
//                                )
//                        ) {
//                            @Override
//                            public void onResponse(Call call, @NonNull Response response) {
//                                if (getStatus(response) == 1)
//                                    getHandler().sendMessage(ConstantsTools.createMessage(1));
//                                else
//                                    getHandler().sendMessage(ConstantsTools.createMessage(getCode()));
//                            }
//                        });
//            }
//        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("userIDExtra", Integer.parseInt(id));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameView;
        TextView contentView;
        TextView timeView;
        ImageButton prizeView;
        ImageButton commentView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            nameView = itemView.findViewById(R.id.Discover_user);
            contentView = itemView.findViewById(R.id.Discover_content);
            timeView = itemView.findViewById(R.id.Discover_time);
            prizeView = itemView.findViewById(R.id.Discover_prize);
            commentView = itemView.findViewById(R.id.Discover_leave_msg);
        }

        void setData(TweetItem item) {
            nameView.setText(item.getUser().getNickname());
            contentView.setText(item.getContent());
            timeView.setText(DateFormat.getInstance().format(new Date(item.getPublishTime())));
        }
    }
}
